<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="文件模版名称">
                <a-input
                  v-model="queryParam.docName"
                  placeholder="请输入部门名称"
                  allow-clear
                  @keyup.enter.native="handleQuery"
                />
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: left">
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />重置</a-button
                >
              </span>
            </a-col>
            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleAdd"
                  ><a-icon type="plus" />新增模版</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-taizhang-model-add-form
        v-if="showAddModal"
        ref="bizTaizhangModelAddForm"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-taizhang-model-edit-form
        v-if="showEditModal"
        ref="bizTaizhangModelEditForm"
        @ok="getList"
        @close="showEditModal = false"
      />
      <a-row :gutter="32">
        <a-col
          :span="3"
          v-for="(item, index) in bizTaizhangModelList"
          :key="index"
          style="margin: 20px 0 0 20px"
        >
          <a-card hoverable>
            <img
              src="@/assets/images/model_doc.png"
              v-if="item.docType == 'WORD'"
              style="width: 70%; margin-left: 15%; margin-top: 10px"
            />
            <img
              src="@/assets/images/model_xls.png"
              v-if="item.docType == 'EXCEL'"
              style="width: 70%; margin-left: 15%; margin-top: 10px"
            />
            <template slot="actions" class="ant-card-actions">
              <a-icon
                key="download"
                type="download"
                @click="handleDownload(item.docUrl, item.docName)"
              ></a-icon>
              <a-icon key="edit" type="edit" @click="handleUpdate(item)" />
              <a-icon key="delete" type="delete" @click="handleDelete(item)" />
            </template>
            <a-card-meta :title="item.docName" style="padding: 10px">
            </a-card-meta>
          </a-card>
        </a-col>
      </a-row>
    </a-card>
    <a-row style="background-color: #fff; margin-top: -40px">
      <a-col :span="24" style="left: 45%">
        <a-pagination
          show-size-changer
          :default-current="3"
          :total="total"
          @showSizeChange="onShowSizeChange"
          @change="changeSize"
          size="middle"
          :show-total="
            (total, range) => `第${range[0]}-${range[1]} 条，总计 ${total} 条`
          "
        />
      </a-col>
    </a-row>
  </div>
</template>
<script>
import {
  listBizTaizhangModel,
  delBizTaizhangModel,
  exportBizTaizhangModel,
} from "@/api/biz/bizTaizhangModel";

import BizTaizhangModelAddForm from "@/views/biz/taizhangmodel/modules/BizTaizhangModelAddForm";
import BizTaizhangModelEditForm from "@/views/biz/taizhangmodel/modules/BizTaizhangModelEditForm";
export default {
  name: "BizTaizhangModel",
  components: {
    BizTaizhangModelAddForm,
    BizTaizhangModelEditForm,
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 台账模版表格数据
      bizTaizhangModelList: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,
      },
      columns: [
        {
          title: "序号",
          dataIndex: "rowindex",
          ellipsis: true,
          align: "center",
          width: "5%",
          scopedSlots: { customRender: "rowindex" },
        },
        {
          title: "创建者",
          dataIndex: "userId",
          ellipsis: true,
          align: "left",
          width: "10%",
        },
        {
          title: "创建部门",
          dataIndex: "deptId",
          ellipsis: true,
          align: "left",
          width: "10%",
        },
        {
          title: "创建名称",
          dataIndex: "userName",
          ellipsis: true,
          align: "left",
          width: "10%",
        },
        {
          title: "部门名称",
          dataIndex: "deptName",
          ellipsis: true,
          align: "left",
          width: "10%",
        },
        {
          title: "公司ID",
          dataIndex: "companyId",
          ellipsis: true,
          align: "left",
          width: "10%",
        },
        {
          title: "模版名称",
          dataIndex: "docName",
          ellipsis: true,
          align: "left",
          width: "10%",
        },
        {
          title: "地址",
          dataIndex: "docUrl",
          ellipsis: true,
          align: "left",
          width: "10%",
        },
        {
          title: "操作",
          dataIndex: "operation",
          align: "center",
          width: "10%",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleDownload(url, name) {
      const ele = document.createElement("a");
      ele.setAttribute("target", "_blank"); // 设置下载文件的url地址
      ele.setAttribute("download", name);
      ele.setAttribute("href", url); // 设置下载文件的url地址
      document.body.appendChild(ele);
      ele.click();
      document.body.removeChild(ele);
    },
    /** 查询台账模版列表 */
    getList() {
      this.loading = true;
      listBizTaizhangModel(this.queryParam).then((response) => {
        this.bizTaizhangModelList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd() {
      this.showAddModal = true;
      this.$nextTick(() => this.$refs.bizTaizhangModelAddForm.handleAdd());
    },
    handleUpdate(record, ids) {
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizTaizhangModelEditForm.handleUpdate(record, ids)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizTaizhangModelIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizTaizhangModel(bizTaizhangModelIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizTaizhangModel(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>
